package com.judian.smart.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.judian.smart.R;
import com.judian.smart.constant.Constants;
import com.judian.smart.holder.DeviceViewHolder;
import com.judian.smart.interfaces.OnListItemClickListener;
import com.judian.smart.model.Device;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: Peter
 * date: 2019/08/05 星期一 18:08
 * desc:
 */
public class DeviceListAdapter extends RecyclerView.Adapter<DeviceViewHolder> {
    private Context mContext;
    private List<Device> mData;

    private static Map<Integer, Integer> sPictureSelect = new HashMap<>(); //选中
    private static Map<Integer, Integer> sPictureNormal = new HashMap<>(); //未选中
    private static List<Integer> sPictureList = new ArrayList();  //窗帘渐变图片

    public OnListItemClickListener<Device> listener = null;

    static {
        sPictureSelect.put(Constants.DEVICE_TYPE_LIGHT, R.mipmap.light_click);
        sPictureSelect.put(Constants.DEVICE_TYPE_CURTAIN, R.mipmap.curtain_click);
        sPictureSelect.put(Constants.DEVICE_TYPE_SOCKET, R.mipmap.socket_click);
        sPictureSelect.put(Constants.DEVICE_TYPE_CONDITIONING, R.mipmap.conditioning_click);
        sPictureSelect.put(Constants.DEVICE_TYPE_TV, R.mipmap.tv_click);
        sPictureSelect.put(Constants.DEVICE_TYPE_INFRARED, R.mipmap.infrared_click);
        sPictureSelect.put(Constants.DEVICE_TYPE_DIMMER, R.mipmap.dimmer_click);
        sPictureSelect.put(Constants.DEVICE_TYPE_MUSIC, R.mipmap.music_click);
        sPictureSelect.put(Constants.DEVICE_TYPE_LOCK, R.mipmap.lock_click);
        sPictureSelect.put(Constants.DEVICE_TYPE_PROJECTOR, R.mipmap.projector_click);
        sPictureSelect.put(Constants.DEVICE_TYPE_INTELLIGENTOPEN, R.mipmap.air_click);
        sPictureSelect.put(Constants.DEVICE_TYPE_TIMESOCKET, R.mipmap.time_click);
        sPictureSelect.put(Constants.DEVICE_TYPE_FRESHAIRSYSTEM, R.mipmap.freshairsystem_click);
        sPictureSelect.put(Constants.DEVICE_TYPE_ELECTRICAL, R.mipmap.electrical_click);
        sPictureNormal.put(Constants.DEVICE_TYPE_SENCER, R.mipmap.infrared_click);
    }

    static {
        sPictureNormal.put(Constants.DEVICE_TYPE_LIGHT, R.mipmap.light_click_not);
        sPictureNormal.put(Constants.DEVICE_TYPE_CURTAIN, R.mipmap.curtain_click_not);
        sPictureNormal.put(Constants.DEVICE_TYPE_SOCKET, R.mipmap.socket_click_not);
        sPictureNormal.put(Constants.DEVICE_TYPE_CONDITIONING, R.mipmap.conditioning_click_not);
        sPictureNormal.put(Constants.DEVICE_TYPE_TV, R.mipmap.tv_click_not);
        sPictureNormal.put(Constants.DEVICE_TYPE_INFRARED, R.mipmap.infrared_click_not);
        sPictureNormal.put(Constants.DEVICE_TYPE_DIMMER, R.mipmap.dimmer_click_not);
        sPictureNormal.put(Constants.DEVICE_TYPE_MUSIC, R.mipmap.music_click_not);
        sPictureNormal.put(Constants.DEVICE_TYPE_LOCK, R.mipmap.lock_click_not);
        sPictureNormal.put(Constants.DEVICE_TYPE_PROJECTOR, R.mipmap.projector_click_not);
        sPictureNormal.put(Constants.DEVICE_TYPE_INTELLIGENTOPEN, R.mipmap.air_click_not);
        sPictureNormal.put(Constants.DEVICE_TYPE_TIMESOCKET, R.mipmap.time_click_not);
        sPictureNormal.put(Constants.DEVICE_TYPE_FRESHAIRSYSTEM, R.mipmap.freshairsystem_click_not);
        sPictureNormal.put(Constants.DEVICE_TYPE_ELECTRICAL, R.mipmap.electrical_click_not);
        sPictureNormal.put(Constants.DEVICE_TYPE_SENCER, R.mipmap.infrared_click_not);
    }

    static {
        sPictureList.add(R.mipmap.device_click);
        sPictureList.add(R.mipmap.jian_bian_9);
        sPictureList.add(R.mipmap.jian_bian_8);
        sPictureList.add(R.mipmap.jian_bian_7);
        sPictureList.add(R.mipmap.jian_bian_6);
        sPictureList.add(R.mipmap.jian_bian_5);
        sPictureList.add(R.mipmap.jian_bian_4);
        sPictureList.add(R.mipmap.jian_bian_3);
        sPictureList.add(R.mipmap.jian_bian_2);
        sPictureList.add(R.mipmap.jian_bian_1);
        sPictureList.add(R.mipmap.device_click_not);
    }


    public DeviceListAdapter(Context context, List<Device> list) {
        this.mContext = context;
        this.mData = list;
    }

    /**
     * 判断是以房间类型分类还是设备类型分类
     * 0 -> 设备 {@code Constants.DEVICE}
     * 2 -> 房间 {@code Constants.HOME}
     */
    public int isRoomOrDevice = Constants.HOME;

    public void setData(List<Device> data) {
        mData = data;
    }

    public void setRoomOrDevice(int isRoomOrDevice) {
        this.isRoomOrDevice = isRoomOrDevice;
    }

    public void setListener(OnListItemClickListener<Device> listener) {
        this.listener = listener;
    }

    @Override
    public void onBindViewHolder(@NonNull DeviceViewHolder holder, final int position) {
        String deviceName;
        switch (isRoomOrDevice) {   //判断当前列表是以什么分类
            case Constants.HOME:
                deviceName = mData.get(position).getDeviceName();
                break;
            case Constants.DEVICE:
                deviceName = mData.get(position).getRoomName() + mData.get(position).getDeviceName();
                break;
            default:
                deviceName = mData.get(position).getDeviceName();
                break;
        }
        holder.deviceName.setText(deviceName);

        //给Item添加点击事件
        holder.deviceLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.setOnItemClick(mData.get(position), position);
            }
        });

        //判断当前设备的状态,来决定使用什么图片
        if (mData.get(position).getDeviceTypeKey() == Constants.DEVICE_TYPE_CURTAIN) {  //窗帘状态
            if (mData.get(position).getLoadImageIndex() != 10) { //开启状态
                holder.deviceName.setTextColor(Color.WHITE);
                holder.deviceImage.setBackgroundResource(sPictureSelect.get(mData.get(position).getDeviceTypeKey()));
            } else {    //关闭状态
                holder.deviceName.setTextColor(Color.rgb(131, 186, 255));
                holder.deviceImage.setBackgroundResource(sPictureNormal.get(mData.get(position).getDeviceTypeKey()));
            }
            holder.deviceLayout.setBackgroundResource(sPictureList.get(mData.get(position).getLoadImageIndex())); //设置窗帘渐变图片
        } else {    //非窗帘状态
            if (mData.get(position).getState() == 1) {     //开启状态
                holder.deviceName.setTextColor(Color.WHITE);
                holder.deviceImage.setBackgroundResource(sPictureSelect.get(mData.get(position).getDeviceTypeKey()));
                holder.deviceLayout.setBackgroundResource(R.mipmap.device_click);
            } else {
                holder.deviceName.setTextColor(Color.rgb(131, 186, 255));
                Device device = mData.get(position);
                int deviceTypeKey = device.getDeviceTypeKey();
                int picId = sPictureNormal.get(deviceTypeKey);
                holder.deviceImage.setBackgroundResource(picId);
                holder.deviceLayout.setBackgroundResource(R.mipmap.device_click_not);
            }
        }
    }

    @NonNull
    @Override
    public DeviceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_device, parent, false);
        view.getLayoutParams().height = 475 / 3 - 38;//设置列表显示三行  -38表示加上间隔
        return new DeviceViewHolder(view);
    }


    @Override
    public int getItemCount() {
        return mData.size();
    }
}
